import UIKit

/* 作业1 */

/* 元组型 */
// 是否在左侧定义各个值的类型并不重要，Swift会根据右边的类型自动处理左边的类型
let studentInfo: (number: Int, name: String, outlook: String) = (22371345, "贾博驿", "共青团员")

// 方法1
let (studentNumber, studentName, studentOutlook) = studentInfo
print("\(studentNumber) - \(studentName) - \(studentOutlook)")

// 方法2
print("\(studentInfo.0) - \(studentInfo.1) - \(studentInfo.2)")

// 方法3
print("\(studentInfo.number) - \(studentInfo.name) - \(studentInfo.outlook)")

/* 可选型 */
// 1
var myHobby: String?
myHobby = "Ukelele"
var myHobby_noHobby: String?
myHobby_noHobby = nil

// 2
var parseStringToInt = Int("10")
// 能推断出变量的类型
// 是Int可选型
// 因为可能输入的不是一个数字，此时应该给变量赋值nil
parseStringToInt = Int("Picasso")
// 此时变量的值为nil

// 3
var name: String = "Tommy"
var hobby: String?
print("name: \(name) hobby: ")
if(hobby != nil) {
    print(hobby!)
}else{
    print("no hobby")
}

// 4
var membership: String? = "IEEE senior member"
if let memberprint = membership {
    print(memberprint)
}else{
    print("no membership")
}

// 5
hobby = "soccer"
if let hobbyprint = hobby, let memberprint = membership {
    print("hobby: \(hobbyprint) membership: \(memberprint)")
}else{
    print("ERROR!")
}

// 6
var getHobby = hobby ?? "No hobby"
var getMembership = membership ?? "No membership"
// getHobby = "soccer"
// getMembership = "IEEE senior member"
hobby = nil
membership = nil
getHobby = hobby ?? "No hobby"
getMembership = membership ?? "No membership"
// getHobby = "No hobby"
// getMembership = "No membership"

/* 字符和字符串 */
// 1
var universityName: String = String()
var collegeName: String = ""

// 2
if(collegeName.isEmpty){
    print("collegeName is empty!")
}else{
    print(collegeName)
}

// 3
universityName = "Beihang University"
if(universityName.isEmpty){
    print("collegeName is empty!")
}else{
    print(collegeName)
}

// 4
var MyUniversity = universityName
print("universityName: \(universityName) MyUniversity: \(MyUniversity)")
MyUniversity = "Tsinghua University"
print("universityName: \(universityName) MyUniversity: \(MyUniversity)")
// 第二次打印结果和第一次结果相比只有MyUniversity有变化
// 在Swift中字符串复制的时候是复制值而非复制指针的
// 就算在变量间相互赋值，各个变量也是相互独立的

/* 字符串操作 */
// 1
var myHobbies = "AB CD EF"
// Swift5中已经移除了characters属性，要求直接使用String变量本身
for char in myHobbies {
    print(char)
}
// 调用8次

// 2
print(myHobbies.count)
// 一致，二者都是对字符串每个字符做了统计

// 3
let str1 = "My hobbies"
let str2 = "is"
var info: String = str1 + str2 + myHobbies
print(info)

// 4
info = "\(str1) \(str2) \(myHobbies)"
print(info)

// 5
// Swift5字符串没有uppercaseString和lowercaseString属性
// 改用uppercased()和lowercased()方法
print(info.uppercased())
print(info.lowercased())

// 6
print(myHobbies[myHobbies.startIndex])
print(myHobbies[myHobbies.index(before: myHobbies.endIndex)])
print(myHobbies[myHobbies.index(myHobbies.startIndex, offsetBy: 3)])
print(myHobbies[myHobbies.index(myHobbies.endIndex, offsetBy: -5)])

// 7
let firstIndex = myHobbies.firstIndex(of: " ")!
let lastIndex = myHobbies.lastIndex(of: " ")!
let myHobbies_1 = myHobbies[myHobbies.startIndex..<firstIndex]
let myHobbies_2 = myHobbies[myHobbies.index(after: firstIndex)..<lastIndex]
let myHobbies_3 = myHobbies[myHobbies.index(after: lastIndex)..<myHobbies.endIndex]
print(myHobbies_1)
print(myHobbies_2)
print(myHobbies_3)

// 8
print(myHobbies_1 == myHobbies_2)
print(myHobbies_1.hasPrefix("A"))
print(myHobbies_2.hasSuffix("E"))
